package com.woniu.darryringmall.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.darryringmall.admin.Request.SpecVO;
import com.woniu.darryringmall.front.entity.SpecValue;
import com.woniu.darryringmall.front.service.SpecValueService;
import com.woniu.darryringmall.utils.ResponseData;
import com.woniu.darryringmall.utils.ResponseUtils;
import com.woniu.darryringmall.utils.ResultStateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController("adminSpecValue")
@RequestMapping("admin/specValue")
public class SpecValueController {
    @Autowired
    private SpecValueService specValueService;

    @RequestMapping("add")
    public ResponseData add(@RequestBody SpecValue specValue) {
        if (specValue.getSpecValue() == null || specValue.getSpecValue().trim().length() < 2) {
            return ResponseUtils.getResponseData(ResultStateCode.FAIL, "操作失败", "规格名的长度必须在2位以上");
        }
        QueryWrapper<SpecValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("specvalue", specValue.getSpecValue());
        queryWrapper.eq("specid", specValue.getSpecId());
        if (specValueService.getOne(queryWrapper) != null) {
            return ResponseUtils.getResponseData(ResultStateCode.FAIL, "操作失败", "规格值名重复");
        }
        specValueService.save(specValue);
        return ResponseUtils.getResponseData(ResultStateCode.SUCCESS, "操作成功", specValue);
    }

    @RequestMapping("list")
    public ResponseData list(int specId) {
        QueryWrapper<SpecValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("specid", specId);
        return ResponseUtils.getResponseData(ResultStateCode.SUCCESS, "操作成功", specValueService.list(queryWrapper));
    }

    @RequestMapping("del")
    public ResponseData del(@RequestParam(value = "ids[]") Integer[] ids) {
        specValueService.removeByIds(Arrays.asList(ids));
        return ResponseUtils.SUCCESS;
    }
}
